package com.example.base.base

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.*
import android.widget.FrameLayout
import androidx.annotation.LayoutRes
import androidx.fragment.app.DialogFragment
import com.example.base.R

open class BaseDialog : DialogFragment{

    private var contentView: View? = null
    @LayoutRes
    private var contentLayoutId: Int? = null

    constructor(): super()
    constructor(@LayoutRes  contentLayoutId: Int): super() {
        this.contentLayoutId = contentLayoutId
    }

    protected open var dialogWidth = ViewGroup.LayoutParams.WRAP_CONTENT
    protected open var dialogHeight = ViewGroup.LayoutParams.WRAP_CONTENT
    protected open var dialogGravity: Int? = null

    private var onDismissAction: (() -> Unit)? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (contentLayoutId != null) {
            val viewRoot = FrameLayout(inflater.context)
            viewRoot.addView(inflater.inflate(contentLayoutId!!, viewRoot, false))
            return viewRoot
        }
        throw NullPointerException("please init contentView or contentLayoutId")
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(dialogWidth, dialogHeight)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        val window = dialog.window
        if (window != null) {
            window.setBackgroundDrawableResource(android.R.color.transparent)
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            window.decorView.setPadding(0, 0, 0, 0)
        }

        dialog.setOnKeyListener { _, _, keyEvent ->
            if (keyEvent.keyCode == KeyEvent.KEYCODE_BACK &&
                keyEvent.repeatCount == 0 &&
                keyEvent.action == KeyEvent.ACTION_UP) {
                return@setOnKeyListener onBackPressed()
            }
            false
        }

        dialogGravity?.let {
            dialog.window?.setGravity(it)
        }

        return dialog
    }


    /**
     *  返回键监听
     */
    open fun onBackPressed(): Boolean {
        return false
    }

    override fun onDestroyView() {
        super.onDestroyView()
        view?.setOnTouchListener(null)
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        onDismissAction?.invoke()
    }

    fun setOnDismissAction(action: () -> Unit): BaseDialog {
        this.onDismissAction = action
        return this
    }

    fun setFullScreen() {

        dialogWidth = ViewGroup.LayoutParams.MATCH_PARENT
        dialogHeight = ViewGroup.LayoutParams.MATCH_PARENT

        if (isAdded) {
            dialog?.window?.setLayout(dialogWidth, dialogHeight)
        }
    }
}